package com.huang.wangpan.strategy.process;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huang.wangpan.dto.FileIm;
import com.huang.wangpan.dao.UserFileDao;
import com.huang.wangpan.dao.UserDao;
import com.huang.wangpan.vo.UserFile;
import com.huang.wangpan.utils.CommonUtils;
import com.huang.wangpan.utils.PathAnalysis;
import com.huang.wangpan.utils.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: liminghui
 * @Date: 2020/11/7 12:37
 * @Version:
 * @Description:
 */
@Component
public class IndexProcess implements CommonProcess {
    @Autowired
    UserFileDao userFileDao;

    @Autowired
    UserDao userDao;

    private static final Logger logger = LoggerFactory.getLogger(IndexProcess.class);

    @Override
    public void process(String method, HttpServletRequest request, HttpServletResponse response) {
        // 获取首页面数据
        ResultData resultData = new ResultData();
        PathAnalysis pathAnalysis = new PathAnalysis();
        String token = CommonUtils.loadToken(request);
        String name = userDao.selectNameByToken(token);
        List<UserFile> userFileList = userFileDao.searchByName(name);
        logger.info("IndexProcess-->process;  name={}", name);
        List<FileIm> ufs1 = pathAnalysis.getIndexPath(userFileList);
        JSONObject message = new JSONObject();
        JSONArray data = new JSONArray();
        for (FileIm temp : ufs1) {
            JSONObject tempj = new JSONObject();
            tempj.put("filesize", temp.getFilesize());
            tempj.put("filename", temp.getFilename());
            tempj.put("date", temp.getMtime());
            tempj.put("iconsign", temp.getIconsign());
            data.add(tempj);
        }
        JSONObject msg = new JSONObject();
        msg.put("Catalogue", 0);
        msg.put("currentpath", "/");
        message.put("code", 0);
        message.put("msg", msg);
        message.put("data", data);
        resultData.setData(message);
        resultData.writeToResponse(response);
        logger.info("IndexProcess-->process; 获取首页数据完成！");
    }
}
